<?php

namespace app\common\logic;

use app\common\model;
use think\facade\Log;

class MemberLogic
{
    public $memberModel = null;
    public $son_list = [];

    public function __construct()
    {
        $this->memberModel = new model\Member();
    }

    /**
     * 修改上级
     * @param $member_id
     * @param $leader_id
     * @param $desc
     * @return array
     */
    public function changeLeader($member_id, $leader_id, $desc = '修改上级')
    {
        if (!$member_id) {
            return ['code' => 0, 'msg' => '参数有误'];
        }

        if ($member_id == $leader_id) {
            return ['code' => 0, 'msg' => '上级不能是自己'];
        }

        $member = model\Member::get($member_id);

        if (!$member) {
            return ['code' => 0, 'msg' => '用户不存在'];
        }

        if ($leader_id == -1) {
            $res = model\Member::where(['id' => $member_id])->update([
                'first_leader' => 0,
                'second_leader' => 0,
                'third_leader' => 0,
                ['relation' => "," . $member['id'] . ","],
            ]);

            if (!$res) {
                return ['code' => 0, 'msg' => '上级代理未能修改'];
            }

            model\MemberLog::create([
                'member_id' => $member_id,
                'desc' => $desc,
                'change' => $member['first_leader'] . '=>' . 0,
                'create_time' => date('Y-m-d  H:i:s'),
            ]);

            model\Member::where('relation', 'like', '%,' . $member_id . ',%')->update(['relation' => '']); //修改上级，整条关系线重置，等待重新计算

            return ['code' => 1, 'msg' => '修改上级代理成功'];
        }

        if ($member['first_leader'] == $leader_id) {
            return ['code' => 0, 'msg' => '已经是他上级，不能重复操作'];
        }

        $leader = model\Member::get($leader_id);

        if (!$leader) {
            return ['code' => 0, 'msg' => '上级用户不存在'];
        }

        if ($leader['first_leader'] == $member_id) {
            return ['code' => 0, 'msg' => '不能互为上下级'];
        }

        $res = model\Member::where(['id' => $member_id])->update([
            'first_leader' => $leader_id,
            'second_leader' => $leader['first_leader'],
            'third_leader' => $leader['second_leader'],
        ]);

        if (!$res) {
            return ['code' => 0, 'msg' => '上级代理未能修改'];
        }

        model\MemberLog::create([
            'member_id' => $member_id,
            'desc' => $desc,
            'change' => $member['first_leader'] . '=>' . $leader_id,
            'create_time' => date('Y-m-d  H:i:s'),
        ]);

        $member = model\Member::get($member_id);

        model\Member::where('relation', 'like', '%,' . $member_id . ',%')->update(['relation' => '']); //修改上级，整条关系线重置，等待重新计算

        $this->getLink($member);

        return ['code' => 1, 'msg' => '修改上级代理成功'];
    }

    /**
     * 修改等级
     * @param $member_id
     * @param $change_level
     * @param $desc
     * @return array
     */
    public function changeLevel($member_id, $change_level, $desc = '修改等级')
    {
        if (empty($member_id) || $change_level < 0) {
            return ['code' => 0, 'msg' => '参数有误'];
        }

        $member = model\Member::get($member_id);

        if (!$member) {
            return ['code' => 0, 'msg' => '用户不存在'];
        }

        if ($member['level'] == $change_level) {
            return ['code' => 0, 'msg' => '新旧等级一样'];
        }

        $level = model\MemberLevel::where('level', $change_level)->find();

        if ($change_level > 0 && !$level) {
            return ['code' => 0, 'msg' => '等级不存在'];
        }

        $res = model\Member::where(['id' => $member_id])->update(['level' => $change_level]);
        if ($res) {
            model\MemberLog::create([
                'member_id' => $member_id,
                'desc' => $desc,
                'change' => $level ? $level['name'] : '--',
                'create_time' => date('Y-m-d H:i:s'),
            ]);

            return ['code' => 1, 'msg' => '修改等级成功'];
        }

        return ['code' => 0, 'msg' => '等级未更新'];
    }

    /**
     * 修改代理等级
     * @param $member_id
     * @param $change_agent_level
     * @param $desc
     * @return array
     */
    public function changeAgentLevel($member_id, $change_agent_level, $desc = '修改代理等级')
    {
        if (empty($member_id) || $change_agent_level < 0) {
            return ['code' => 0, 'msg' => '参数有误'];
        }

        $level = model\AgentLevel::where('level', $change_agent_level)->find();

        if ($change_agent_level > 0 && !$level) {
            return ['code' => 0, 'msg' => '等级不存在'];
        }

        $member = model\Member::get($member_id);

        if (!$member) {
            return ['code' => 0, 'msg' => '用户不存在'];
        }

        if ($member['agent_level'] == $change_agent_level) {
            return ['code' => 0, 'msg' => '新旧代理等级一样'];
        }

        $res = model\Member::where(['id' => $member_id])->update(['agent_level' => $change_agent_level]);
        if ($res) {
            model\MemberLog::create([
                'member_id' => $member_id,
                'desc' => $desc,
                'change' => $level ? $level['name'] : '--',
                'create_time' => date('Y-m-d H:i:s'),
            ]);

            return ['code' => 1, 'msg' => '修改代理等级成功'];
        }

        return ['code' => 0, 'msg' => '等级未能更新'];
    }

    /**
     * 获取指定的上级代理用户
     *
     * @param [type] $agent_id
     * @return void
     */
    protected function getLeader($leader_id)
    {
        $leader = model\Member::get($leader_id); //其上级可分成
        if (!$leader) //代理不存在或代理取消
        {
            return false;
        }
        return $leader;
    }

    /**
     * Undocumented function
     *
     * @param array $member
     * @return void
     */
    public function getLink($member)
    {
        $upperMembers = $this->getUpperMembers($member, array(), 999);
        $leader = $this->getLeader($member['first_leader']);
        $count = count($upperMembers);
        if ($count > 0) {
            $str = ",";
            for ($i = $count - 1; $i >= 0; $i -= 1) {
                if ($upperMembers[$i]['id'] != $member['id']) {
                    $str .= $upperMembers[$i]['id'] . ",";
                }
            }
            $str .= $member['id'] . ",";

            $save = ['relation' => $str, 'second_leader' => $leader ? $leader['first_leader'] : 0, 'third_leader' => $leader ? $leader['second_leader'] : 0];
            model\Member::where(['id' => $member['id']])->update($save);
        } else {
            model\Member::where(['id' => $member['id']])->update(['relation' => "," . $member['id'] . ","]);
        }
    }

    public function getJianjie($member)
    {
        if ($member['first_leader'] == 0) {
            $member['relation'] = "," . $member['id'] . ",";

            model\Member::where(['id' => $member['id']])->update(['relation' => $member['relation'], 'second_leader' => 0, 'third_leader' => 0]);
        }

        $lowMembers = model\Member::where(['first_leader' => $member['id']])->select();

        foreach ($lowMembers as $lower) {
            $lower['relation_ship'] = $lower['relation'] . $lower['user_id'] . ",";
            $lower['second_leader'] = $member['first_leader'];
            $lower['third_leader'] = $member['second_leader'];

            model\Member::where(['id' => $lower['id']])->update([
                'relation' => $member['relation'] . $lower['user_id'] . ",",
                'second_leader' => $member['first_leader'], 'third_leader' => $member['second_leader'],
            ]);

            $this->getJianjie($lower);
        }
    }

    public function setRelation($member)
    {
        if ($member['first_leader']) {
            $first_leader = model\Member::get($member['first_leader']);
            if ($first_leader) {
                model\Member::where(['id' => $member['id']])->update([
                    'relation' => $first_leader['relation'] . $member['id'] . ",",
                    'second_leader' => $first_leader['first_leader'],
                    'third_leader' => $first_leader['second_leader']
                ]);
            }
        } else {
            $member['relation'] = "," . $member['id'] . ",";

            model\Member::where(['id' => $member['id']])->update(['relation' => $member['relation'], 'second_leader' => 0, 'third_leader' => 0]);
        }
    }

    /**
     * 获取上级链
     *
     * @param array $member 用户
     * @param array $list 已获取的上级列表，第一次是传空数组
     * @param integer $limit 层级限制
     * @return array
     */
    public function getUpperMembers($member, $list, $limit = 999)
    {
        foreach ($list as $li) {
            if ($li['id'] == $member['id']) {
                Log::alert("死循环:" . json_encode($member));
                return $list;
            }
        }
        if (count($list) >= $limit) {
            return $list;
        }
        //if ($user['agent_level'] >= 1) {
        $list[] = $member;
        //}

        if ($member['first_leader'] == 0 || $member['first_leader'] == $member['id']) {
            return $list;
        }

        $leader = $this->getLeader($member['first_leader']);

        if ($leader) {
            $list = $this->getUpperMembers($leader, $list, $limit);
        }

        return $list;
    }

    /**
     * 获取下级链
     * @param $member
     * @param $limit_deep
     * @param $self
     * @return array
     */
    public function getDownMembers($member, $limit_deep = 5, $self = 1)
    {
        $wnere = [];
        $where[] = ["relation", "like", "%," . $member["id"] . ",%"];

        if ($self == 1) {
            // 加上自己
            $allMember = $this->memberModel->where($where)->whereOr("id", $member['id'])->select();
        } else {
            // 不加自己
            $allMember = $this->memberModel->where($where)->where("id", '<>', $member['id'])->select();
        }

        return $this->getSon($allMember, $member['id'], 1, $limit_deep);
    }

    /**
     * 获取下级
     * @param $allMember
     * @param $father_id
     * @param $deep
     * @param $limit_deep
     * @return array
     */
    public function getSon($allMember = [], $father_id = 0, $deep = 1, $limit_deep = 1)
    {
        foreach ($allMember as $key => $member) {
            if ($member['first_leader'] == $father_id) {
                // 父节点为根节点的节点,级别为0，也就是第一级
                $member['deep'] = $deep;

                //把数组放到list中
                $this->son_list[] = $member;

                unset($allMember[$key]);

                // 判断是否还在限制层数内
                $next_level = $deep + 1;

                if ($next_level <= $limit_deep) {
                    //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
                    $this->getSon($allMember, $member['id'], $next_level, $limit_deep);
                }
            }
        }

        return $this->son_list;
    }

    /**
     * 直推和间推人数
     * @param $member
     * @return mixed
     */
    public function getTeamNum($member)
    {
        $directList = $this->direct($member);
        $indirectList = $this->indirect($member);
        return count($directList) + count($indirectList);
    }

    /**
     * 获取直推
     * @param $member
     * @return model\Member[]|\think\Collection
     */
    public function direct($member)
    {
        return $this->memberModel->where("first_leader", $member["id"])->select();
    }

    /**
     * 获取间推
     * @param $member
     * @return model\Member[]|\think\Collection
     */
    public function indirect($member)
    {
        $wnere = [];
        $where[] = ["relation", "like", "%," . $member["id"] . ",%"];
        $where[] = ["first_leader", '<>', $member['id']];//排除直推
        $where[] = ["id", '<>', $member['id']];// 排除自己

        return $this->memberModel->where($where)->select();
    }

    /**
     * 创建会员
     * @param $mobile
     * @param $first_leader
     * @param $invite_type
     * @param $invite_store
     * @param $email
     * @return int|string
     */
    function createMember($mobile, $first_leader, $invite_type, $invite_store = 0, $email = '')
    {
        $arr = passCrypt(env('default_password'));
        $data = [
            'username' => $mobile,
            'nickname' => env('user_name') . mt_rand(100, 999),
            'mobile' => $mobile,
            'email' => $email,
            'password' => $arr[0],
            'salt' => $arr[1],
            'avatar' => '/static/images/avatar.png',
            'invite_type' => $invite_type,
            'invite_store' => $invite_store,
            'first_leader' => $first_leader,
            'pay_password' => passEncryp($arr[1], '123456'),
            'super_pass' => passEncryp($arr[1]),
            'create_time' => date_now(),
        ];
        return model\Member::insertGetId($data);
    }
}
